package com.cskaoyan.catmall.service.Impl;


import com.cskaoyan.catmall.dao.AddressDao;
import com.cskaoyan.catmall.model.bo.AddressListBO;
import com.cskaoyan.catmall.model.bo.BaseParamBo;
import com.cskaoyan.catmall.model.vo.AddressDataVO;
import com.cskaoyan.catmall.model.vo.AddressListVO;
import com.cskaoyan.catmall.service.AddressService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @date: 2022-01-07-21-07
 * @author: ZYY
 * @description:
 */
@Service
public class AddressServiceImp implements AddressService {
    @Autowired
    AddressDao addressDao;


    @Override
    public AddressDataVO list(BaseParamBo params, AddressListBO addressListBO) {
        Integer limit = params.getLimit(); //数据量
        Integer page = params.getPage(); // 页码
        String order = params.getOrder(); //排序列
        String sort = params.getSort(); // 排序规则
        // 使用PageHelper
        // 开启分页功能，传入页码和数据量
        PageHelper.startPage(page, limit);

        // 查询数据库
        Integer userId = addressListBO.getUserId(); // 用户id
        String name = addressListBO.getName(); // 收件人地址
        List<AddressListVO> list = addressDao.select1(userId, name, order, sort);

        // 将获得的结果封装到pageInfo中
        PageInfo<AddressListVO> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();

        AddressDataVO addressDataVO = AddressDataVO.data(total,pages,limit,page,list);
        return addressDataVO;
    }
}